<?php
require_once 'feedgen/FeedGenerator.php';

class LeproRSS{
	
	/*
	 * Read login capcha
	 */
	public function getCapcha(){
		$url = "http://leprosorium.ru";
	    $str = $this->get_url_contents($url);		
		
		preg_match("/alt=\"captcha\" src=\"\/captchaa\/(.*?)\"/", $str, $matches);
		
	    return $matches[1];
	}
	
	/*
	 * Login into lepra and save cookies
	 */
	public function doLogin($user, $pass, $capcha,$logincode){
		$url = "http://leprosorium.ru/login/";
		$vars = "user=".$user."&pass=".$pass."&captcha=".$capcha;
		$vars .= "&logincode=".$logincode."&save=1&use_ip=1";
		
		$ch = curl_init();
		curl_setopt ($ch, CURLOPT_URL,$url);
		curl_setopt ($ch, CURLOPT_COOKIEFILE, "cookies/".$user.'-'.$logincode);
		curl_setopt ($ch, CURLOPT_COOKIEJAR, "cookies/".$user.'-'.$logincode);
		curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 30);
		curl_setopt ($ch, CURLOPT_POST, 1);
		curl_setopt ($ch, CURLOPT_POSTFIELDS, $vars);
		curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
		$ret = curl_exec ($ch);
		curl_close ($ch);
		
		preg_match('/<div id="content"/', $ret, $matches);
		if(count($matches) > 0){
			return $ret;
		}else{
			return false;
		}
	}
	
	/*
	 * Generate rss feed for lepra
	 */
	public function generateFeed($cookiejar){
		$url = "http://leprosorium.ru/archive/".date("Ymd");
		
		$ch = curl_init();
		curl_setopt ($ch, CURLOPT_URL,$url);
		curl_setopt ($ch, CURLOPT_COOKIEFILE, "cookies/".$cookiejar);
		curl_setopt ($ch, CURLOPT_COOKIEJAR, "cookies/".$cookiejar);
		curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 30);
		curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
		$ret = curl_exec ($ch);
		curl_close ($ch);
				
		// create chanel
		$feeds=new FeedGenerator;
		$feeds->setGenerator(new AtomGenerator); # or AtomGenerator
		$feeds->setAuthor('babka_sotona@leprosorium.ru');
		$feeds->setTitle('LeproRSS');
		$feeds->setChannelLink('http://lepra.m3w.ru/');
		$feeds->setLink('http://leprosorium.ru');
		$feeds->setDescription('Leprosorium.ru news RSS agregator');
		$feeds->setID('http://lepra.m3w.ru/');
						
		
		// get all posts
		preg_match_all("/<div class=\"post.+?\".+?>(.+?)<\/em>/s", $ret, $matches);
		
		foreach($matches[0] as $post){
			$patterns[0] = '/\n+/';
			$patterns[1] = '/\t+/';
			$patterns[2] = '/\r+/';
			$post = preg_replace($patterns,'', $post);
			
			// post text
			preg_match("/<div class=\"dt\">(.+?)<\/div>/", $post, $text);
			$text = $text[1];
			
			// post username and date
			preg_match("/class=\"js-user_login\">(.+?)<\/a>(.+?)<span>/s", $post, $username);
			// save date
			$date = $username[2];//$this->UnixTime();
			// remove unneeded chars
			$date = substr($date,2);
			// replace 'today' string for date-time stamp
			$date = str_replace('сегодня', date("d.m.Y"), $date);
			$date = str_replace('вчера', date('d.m.Y', mktime(0, 0, 0, date("m") , date("d") - 1, date("Y"))), $date);
			// parse date-time stamp to parts
			preg_match('/(\d{2}).(\d{2}).(\d{4}) в (\d{2}).(\d{2})/', $date, $timestamp);
			$hour = $timestamp[4];
			$min = $timestamp[5];
			$month = $timestamp[2];
			$day = $timestamp[1];
			$year = $timestamp[3];
			$sec = '00';
			// generate date
			$date = date("c", mktime($hour,$min,$sec,$month, $day, $year) );
			// save name
			$username = $username[1];
			
			// post comments and link
			preg_match("/<a href=\"\/comments\/(.+?)\">(.+?)<\/a>/", $post, $comments);
			$link = 'http://leprosorium.ru/comments/'.$comments[1];
			$comments = str_replace('комментировать', 'нет комментариев', $comments[2]);
			
			// post rating
			preg_match("/<em>(.+?)<\/em>/", $post, $rate);
			$rate = $rate[1];
			
			// make title
			$to = strpos($text,'<br>');
			if ($to < 1 || $to > 128) $to = 128;
			$titletext = htmlspecialchars( mb_substr( strip_tags($text), 0, $to, 'utf-8') );
			$title = $titletext."... ";
			$title .= "[by ".$username.", ".$comments.", ".$rate."]";

			// fill feed
			$feeds->addItem(new FeedItem($link, $title, $link, $text, $date));
		}
		
		// create new feed
		$feeds->display();
	}
	
	/*
	 * Get url content with forwarding
	 */
	private function get_url_contents($url){
		$crl = curl_init();
		$timeout = 5;
		curl_setopt ($crl, CURLOPT_URL,$url);
		curl_setopt ($crl, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt ($crl, CURLOPT_FOLLOWLOCATION, true);
		curl_setopt ($crl, CURLOPT_CONNECTTIMEOUT, $timeout);
		$ret = curl_exec($crl);
		curl_close($crl);
		return $ret;
	}
}

?>
